using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace TypeAndRun
{
    public class Command
    {
        public string file = null;
        Dictionary<string, string> cmd=new Dictionary<string,string>();

        public Command(string filename)
        {
            string path = System.Environment.CurrentDirectory.ToString();
            filename = path + "\\" + filename;
            if (File.Exists(filename))
            {
                file = filename;
                LoadCommand();
            }
        }

        public string GetValueByKey(string key)
        {
            if (file == null || key == null || key == "" || cmd[key] == "" || cmd[key] == null)
                return "";
            return cmd[key];
        }

        public void LoadCommand()
        {
            StreamReader sr = File.OpenText(file);
            string s = "";
            string key = "";
            string value = "";
            while ((s = sr.ReadLine()) != null)
            {
                int flag = s.IndexOf('=');
                key = s.Substring(0, flag);
                value = s.Substring(flag + 1, s.Length - flag - 1);
                if (!key.Equals("") && !key.Equals(""))
                {
                    cmd[key] = value;
                }
            }
            sr.Close();
        }

        public void Save()
        {
            StreamWriter sw = File.CreateText(file);
            foreach (string key in cmd.Keys)
            {
                sw.WriteLine(key + "=" + cmd[key]);
            }
            sw.Close();
        }

        public void AddCommand(string key, string value)
        {
            cmd[key] = value;
        }

        public void ModifyCommand(string key, string newvalue)
        {
            cmd[key] = newvalue;
        }
    }
}
